package user.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import user.dao.SystemAdminDao;
import user.model.User;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserManagerFrame extends JFrame {

	private JPanel contentPane;
	private JTextField userId;
	private JTextField userName;
	private JTable table;
	private JTextField userIdTxt;
	private JTextField userNameTxt;
	private JTextField password;
	private JButton button1;
	private JButton button2;
	private JButton button3;
	private JButton button4;
	private JButton button5;
	private SystemAdminDao systemAdminDao = new SystemAdminDao();
	private JButton btnNewButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserManagerFrame frame = new UserManagerFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UserManagerFrame() {
		setTitle("用户管理");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 787, 780);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel label2 = new JLabel("用户名");
		label2.setFont(new Font("宋体", Font.PLAIN, 18));
		
		JLabel label1 = new JLabel("账号");
		label1.setFont(new Font("宋体", Font.PLAIN, 18));
		
		userId = new JTextField();
		userId.setColumns(10);
		
		userName = new JTextField();
		userName.setColumns(10);
		
		JButton button6 = new JButton("查询");
		button6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SelectUserActionPerformed(e);
			}
		});
		button6.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel label3 = new JLabel("账号");
		label3.setFont(new Font("宋体", Font.PLAIN, 18));
		
		JLabel label5 = new JLabel("用户名");
		label5.setFont(new Font("宋体", Font.PLAIN, 18));
		
		JLabel label4 = new JLabel("密码");
		label4.setFont(new Font("宋体", Font.PLAIN, 18));
		
		userIdTxt = new JTextField();
		userIdTxt.setEditable(false);
		userIdTxt.setColumns(10);
		
		userNameTxt = new JTextField();
		userNameTxt.setColumns(10);
		
		password = new JTextField();
		password.setColumns(10);
		
		button1 = new JButton("重置");
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset();
			}
		});
		button1.setFont(new Font("宋体", Font.PLAIN, 20));
		
		button2 = new JButton("修改");
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					UpdateUserActionPerformed(e);
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(null, "请选择一行！");
				}
			}
		});
		button2.setFont(new Font("宋体", Font.PLAIN, 20));
		
		button3 = new JButton("删除");
		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeleteUserActionPerformed(e);
			}
		});
		button3.setFont(new Font("宋体", Font.PLAIN, 20));
		
		button4 = new JButton("添加新用户");
		button4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new UserAddDialog().setVisible(true);
				
			}
		});
		button4.setFont(new Font("宋体", Font.PLAIN, 20));
		
		button5 = new JButton("返回主页面");
		button5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new SystemAdminFrame().setVisible(true);
				dispose();
			}
		});
		button5.setFont(new Font("宋体", Font.PLAIN, 20));
		
		btnNewButton = new JButton("刷新");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fillTable("", "");
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 18));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 713, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(51)
									.addComponent(label1)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(userId, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
									.addComponent(label2)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(userName, GroupLayout.PREFERRED_SIZE, 165, GroupLayout.PREFERRED_SIZE)
									.addGap(86))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(29)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(label4)
											.addGap(42)
											.addComponent(password))
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(label5)
											.addGap(24)
											.addComponent(userNameTxt))
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(label3)
											.addGap(42)
											.addComponent(userIdTxt, GroupLayout.PREFERRED_SIZE, 298, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_contentPane.createSequentialGroup()
											.addComponent(button1, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
											.addGap(32)
											.addComponent(button2, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
											.addGap(36)
											.addComponent(button3, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)))
									.addPreferredGap(ComponentPlacement.RELATED, 172, Short.MAX_VALUE)))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(button6)
								.addComponent(btnNewButton)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
									.addComponent(button5)
									.addComponent(button4, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)))))
					.addGap(32))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(28)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(button6)
						.addComponent(label2)
						.addComponent(userName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label1)
						.addComponent(userId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(20)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 314, GroupLayout.PREFERRED_SIZE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(userIdTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label3))
							.addGap(28)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label5))
							.addGap(18))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(7)
							.addComponent(btnNewButton)
							.addGap(120)))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(Alignment.LEADING, gl_contentPane.createParallelGroup(Alignment.BASELINE)
							.addComponent(label4)
							.addComponent(password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
							.addComponent(button4, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
							.addGap(38)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(button5, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
								.addComponent(button1, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
								.addComponent(button2, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
								.addComponent(button3, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))))
					.addGap(58))
		);
		
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				UserMousePressed(e);
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u8D26\u53F7", "\u7528\u6237\u540D", "\u5BC6\u7801"
			}
		));
		scrollPane.setViewportView(table);
		contentPane.setLayout(gl_contentPane);
		this.setLocationRelativeTo(null);
		fillTable("", "");
	}

	protected void UpdateUserActionPerformed(ActionEvent e) {
		String id = this.userIdTxt.getText();
		String userName = this.userNameTxt.getText();
		String password = this.password.getText();
		User user = new User(id, userName, password);
		try {
			int i = systemAdminDao.updateUser(user);
			if(i>0) {
				JOptionPane.showMessageDialog(null, "修改成功");
				reset();
				fillTable("", "");
			}else {
				JOptionPane.showMessageDialog(null, "修改失败");
				}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		
	}


	protected void DeleteUserActionPerformed(ActionEvent e) {
		String id = this.userIdTxt.getText();
		String userId = id;
		try {
			int i = systemAdminDao.deleteUser(userId);
			if(i>0) {
				JOptionPane.showMessageDialog(null, "删除成功！");
				reset();
				fillTable("", "");
			}
			else {
				JOptionPane.showMessageDialog(null, "删除失败！");
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		
	}

	protected void reset() {
		userIdTxt.setText("");
		userNameTxt.setText("");
		password.setText("");
		
	}

	protected void SelectUserActionPerformed(ActionEvent e) {
		String userId = this.userId.getText();
		String userName = this.userName.getText();
		fillTable(userId,userName);
		
	}

	protected void fillTable(String userId, String userName) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.setRowCount(0);
		List<User> userList = systemAdminDao.User(userId, userName);
		for(User user:userList) {
			Vector v = new Vector<>();
	    	v.add(user.getId());
	    	v.add(user.getUserName());
	    	v.add(user.getPassword());
	    	dtm.addRow(v);
		}
		
	}

	protected void UserMousePressed(MouseEvent e) {
		int row = table.getSelectedRow();//获得行号
		userIdTxt.setText(String.valueOf(table.getValueAt(row, 0)));
		userNameTxt.setText((String)table.getValueAt(row, 1));
		password.setText((String)table.getValueAt(row, 2));
		
	}
}
